package org.example.novel.security.handler;
;
import org.example.novel.utlis.BeanHelper;
import org.example.novel.utlis.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 权限异常处理类
 */
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        Result<Object> fail = Result.fail(403,"权限不足,无法操作");

        httpServletResponse.setCharacterEncoding("UTF-8");

        httpServletResponse.setContentType("application/json;charset=utf-8;");

        String s = BeanHelper.toJsonStr(fail);

        httpServletResponse.getWriter().write(s);
    }
}
